import Koa from 'koa'
import { koaBody } from 'koa-body'
import error from 'koa-json-error'
import parameter from 'koa-parameter'
import koaStatic from 'koa-static'
import routing from './router/index.js'
import { getPath } from './utils.js'
import { formatErrorStatus } from './lib/format-error.js'

const app = new Koa()

// 设置图片地址
app.use(koaStatic(getPath('public', 'dir')))

// 错误信息
app.use(error(formatErrorStatus))

// body解析
app.use(koaBody({
    multipart: true,
    formidable: {
        uploadDir: getPath('public/uploads', 'dir'),
        keepExtensions: true // 保留格式
    }
}))

// 校验
app.use(parameter(app))

// 路由中间件
routing(app)

// 监听
app.listen(3002, () => {
    console.log('server is running')
})
